//
//  PlayerViewController.m
//  BobNews
//
//  Created by lanou3g on 16/5/17.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "PlayerViewController.h"
#import "NetWorkRequestManager.h"
#import "PlayerDetailsModel.h"
#import "PlayerDetailsTableViewCell.h"
#import "VideoListModelCell.h"
#import "VideoPlayView.h"
#import "PlayerDetailsTableViewCell.h"


@interface PlayerViewController ()<didClickPlayerDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (weak, nonatomic) IBOutlet UITextField *commentTextField;


@property (nonatomic,strong) NSMutableArray *commentsArray;

@property (nonatomic,strong) VideoPlayView *playerView;
@property (nonatomic,strong) VideoListModelCell *currentCell;


@end

@implementation PlayerViewController
- (IBAction)senderCommentBtu:(id)sender {
    self.commentTextField.text = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    

    self.commentsArray = [NSMutableArray new];
    
    [self dataLoad];
    
    [self.myTableView registerNib:[UINib nibWithNibName:@"VideoListModelCell" bundle:nil] forCellReuseIdentifier:@"VideoListModel"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"PlayerDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:@"PlayerCell"];
    
    
    
}

-(void)dataLoad{
    NSString *str = [NSString stringWithFormat:@"http://comment.api.163.com/api/json/post/list/new/normal/video_bbs/%@/desc/0/5/10/2/2",self.groupID];

    [NetWorkRequestManager requestUrl:[NSURL URLWithString:str] RequestMethod:GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {

        NSMutableDictionary *JsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *newArr = JsonDict[@"newPosts"];

        for (NSDictionary *listDict in newArr) {
            NSDictionary *dict1 = listDict[@"1"];
            
            PlayerDetailsModel *model = [[PlayerDetailsModel alloc]init];
            [model setValuesForKeysWithDictionary:dict1];
            [self.commentsArray addObject:model];

        }
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            [self.myTableView reloadData];
            
        });
        
    }];
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 1:
        {
            return @"评论";
        }
            break;
            
        default:
            break;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 300;
    }
    
    if (indexPath.section == 1) {
        return 180;
    }
    return 0;
}

-(void)clickVideoButton:(NSIndexPath *)indexPath{
    [self.playerView resetPlayView];
    VideoListModelCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
    
    self.currentCell = cell;
    VideoPlayView *playerView = [VideoPlayView videoPlayView];
    VideoListModel *model = self.videoArray[self.index];
    playerView.frame = CGRectMake(10, 49, 398, 193);
    [cell addSubview:playerView];
    cell.playerView = playerView;
    self.playerView = playerView;
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:model.mp4_url]];
    self.playerView.playerItem = item;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    else{
        return self.commentsArray.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        VideoListModelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoListModel" forIndexPath:indexPath];
        cell.model = self.videoArray[self.index];
        
        cell.indexPath = indexPath;
        cell.playerDelegate = self;
        return cell;
        
    }
    
    if (indexPath.section == 1) {
  PlayerDetailsTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"PlayerCell" forIndexPath:indexPath];
        cell1.model = self.commentsArray[indexPath.row];
        
        
        return cell1;
    }
    return nil;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
